import  requests
import xlwt
def saveExcel(sheetname,title,data,filename):
    wbt = xlwt.Workbook()
    sheet = wbt.add_sheet(sheetname)
    for i in range(len(title)):
        sheet.write(0, i, title[i])
    for m in range(len(data)):
        for n in range(len(data[m])):
            sheet.write(m+1, n, data[m][n])
    wbt.save(filename)

url_string =r'https://douban.uieee.com/v2/movie/in_theaters'
response = requests.get(url_string)
obj = response.json()
xdata = []

for move_list in obj['subjects']:
    # move_list = obj['subjects'][0]
    # print(move_list)

    film_name = move_list['title']
    # print(move_list['title'])

    score = move_list['rating']['average']
    # print(move_list['rating']['average'])
    ss=[]
    for i in move_list['casts']:
        ss.append(i['name'])

    film_type = move_list['genres']
    # print(move_list['genres'])

    release_time = move_list['pubdates']
    # print(move_list['pubdates'])

    total_time = move_list['durations']
    # print(move_list['durations'])

    data = [film_name]+[score]+[ss]+[film_type]+[release_time]+[total_time]
    xdata.append(data)
print(xdata)
if __name__ == '__main__':
    title1 = ['电影名称', '评分', '主演', '类型', '上映时间', '总时间']
    data1 = xdata
    sheetname1 = '电影'
    filename1 = '电影数据.xls'
    saveExcel(sheetname1, title1, data1, filename1)